﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.InputOutput.Encryption
Imports AugmentLib.WebComponent

Partial Class RenReportView
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("StaffId") Is Nothing Then
            Response.Redirect("Default.aspx")
        Else
            lblStaffUID.Text = Session("StaffId").ToString
            lblStaffType.Text = Session("StaffType").ToString
        End If

        If Not Page.IsPostBack Then

            CBEBack.ConfirmText = "You are about to navigate away from this page. " & vbCrLf & "Changes will be discard!"

            'Dim ListType As String
            If Not Request.QueryString("id") Is Nothing Then
                lblRenContactId.Text = SymmetricDecryptFromHex(Request.QueryString("id"), "augment")
                LoadRenContact()
                LoadProperty()
                LoadRenReports()
            Else
                Response.Redirect("Default.aspx")
            End If
        End If
    End Sub

    Private Sub LoadRenContact()
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        objDB.OpenConnection()
        objDB.dbVariable.SQLString = "SELECT Name, Phone, Mobile, Fax, Company, Email, Address, CASE Status " & _
                                    "WHEN '0' THEN 'ACTIVE' WHEN '1' THEN 'INACTIVE' WHEN '2' THEN 'ON-HOLD' WHEN '3' THEN 'CLOSED' ELSE '' END as Status " & _
                                    "FROM AW_RenContact WHERE RenContactId=@RenContactId AND StatusDelete=0"
        objDB.AddParameter("RenContactId", Data.SqlDbType.UniqueIdentifier, New Guid(lblRenContactId.Text))
        objDB.FillData(TempDT, "AW_RenContact", Database.SQLCommandType.NormalString)

        objDB.CloseConnection()

        For Each ReadRow As Data.DataRow In TempDT.Rows
            lblName.Text = ReadRow.Item("Name").ToString
            lblPhone.Text = ReadRow.Item("Phone").ToString
            lblMobile.Text = ReadRow.Item("Mobile").ToString
            lblFax.Text = ReadRow.Item("Fax").ToString
            lblCompany.Text = ReadRow.Item("Company").ToString
            lblEmail.Text = ReadRow.Item("Email").ToString
            lblAddress.Text = ReadRow.Item("Address").ToString
            lblStatus.Text = ReadRow.Item("Status").ToString
        Next
        objDB.CloseConnection()
    End Sub

    Private Sub LoadProperty()
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        objDB.OpenConnection()
        objDB.dbVariable.SQLString = "SELECT AC.Country, S.State, AL.Location, AR.Road, AP.Project, AT.Type, " & _
                                    "CASE RP.ListType WHEN '0' THEN 'SR - Sales Residential' " & _
                                    "WHEN '1' THEN 'SC - Sales Commercial' WHEN '2' THEN 'SI - Sales Industrial' " & _
                                    "WHEN '3' THEN 'SL - Sales Land' WHEN '4' THEN 'RR - Rent Residential' " & _
                                    "WHEN '5' THEN 'RC - Rent Commercial' WHEN '6' THEN 'RI - Rent Industrial' " & _
                                    "WHEN '7' THEN 'RL - Rent Land' ELSE '' END as ListType, PriceFrom, PriceTo FROM " & _
                                    "AW_RenProperty RP LEFT JOIN AW_Country AC ON RP.CountryId = AC.CountryId " & _
                                    "LEFT JOIN AW_State S ON RP.StateId = S.StateId LEFT JOIN AW_Location AL ON " & _
                                    "RP.LocationId = AL.LocationId LEFT JOIN AW_Road AR ON RP.RoadId = AR.RoadId " & _
                                    "LEFT JOIN AW_Project AP ON RP.ProjectId = AP.ProjectId LEFT JOIN AW_PropertyType AT " & _
                                    "ON RP.PropertyTypeId = AT.TypeId WHERE RP.RenContactId=@RenContactId"
        objDB.AddParameter("RenContactId", Data.SqlDbType.UniqueIdentifier, New Guid(lblRenContactId.Text))
        objDB.FillData(TempDT, "AW_RenProperty", Database.SQLCommandType.NormalString)

        objDB.CloseConnection()
        repRenProperty.DataSource = TempDT
        repRenProperty.DataBind()
    End Sub

    Private Sub LoadRenReports()
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        objDB.OpenConnection()
        objDB.dbVariable.SQLString = "SELECT CONVERT(NVARCHAR(11),CAST(RenDate AS NVARCHAR(2)) " & _
                                    "+ '-' +CAST(RenMonth AS NVARCHAR(2))+ '-' +CAST(RenYear AS NVARCHAR(4)) ,103) AS Date, " & _
                                    "Title, Remarks, BossRemarks, RenReportId FROM AW_RenReport WHERE RenContactId=@RenContactId AND " & _
                                    "StatusDelete=0 ORDER BY Date"
        objDB.AddParameter("RenContactId", Data.SqlDbType.UniqueIdentifier, New Guid(lblRenContactId.Text))
        objDB.FillData(TempDT, "AW_RenReport", Database.SQLCommandType.NormalString)

        objDB.CloseConnection()
        RepRenReport.DataSource = TempDT
        RepRenReport.DataBind()
    End Sub

    Protected Sub RepRenReport_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles RepRenReport.ItemCommand
        If e.CommandName.ToUpper = "EDITITEM" Then
            Dim strBack As String
            strBack = "RenReportView.aspx?id=" & SymmetricEncryptToHex(lblRenContactId.Text.Trim, "augment")
            Session.Add("Back", strBack)
            Dim RenReportUID As String = CType(RepRenReport.Items(e.CommandArgument).FindControl("lblRepRenReportId"), Label).Text
            Response.Redirect("RenReportEntry.aspx?id=" & SymmetricEncryptToHex(RenReportUID, "augment"))
        End If
    End Sub

    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBack.Click
        If Session("Back") Is Nothing Then
            Response.Redirect("RenReportSearch.aspx")
        Else
            Dim strBack As String = Session("Back").ToString
            Dim arr() As String = strBack.Split("ڡ")
            Response.Redirect(arr(0).ToString)
        End If
    End Sub
End Class
